package cm.mefobe.android.telecomcost.dialog;

import java.util.ArrayList;
import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.DatabaseManager;
import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.activities.DailyThresholdListActivity;
import cm.mefobe.android.telecomcost.adapter.DailyThresholdEditAdapter;
import cm.mefobe.android.telecomcost.entities.DailyThreshold;
import cm.mefobe.android.telecomcost.entities.MonthlyThreshold;
import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class DailyThresholdDialog extends Dialog {

	private DatabaseManager db;
	private boolean shouldRefreshThresholdList = false;
	private Context _context;
	private ListView threshold_list;
	private Button btnClose;
	private Button btnSave;
	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public DailyThresholdDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		_context = context;
	}

	/**
	 * @param context
	 * @param theme
	 */
	public DailyThresholdDialog(Context context, int theme) {
		super(context, theme);
		_context = context;
	}

	public DailyThresholdDialog(Context context) {
		super(context);
		_context = context;
	}

	private void saveDailyThreshold(){		
		Log.i("DailyThresholdDialog", "saving DailyThreshold...");		
		
		DailyThresholdListActivity activity = (DailyThresholdListActivity) _context;
		ArrayList<DailyThreshold> thresholds = activity.getThresholds();

		DailyThresholdEditAdapter adapter = (DailyThresholdEditAdapter)threshold_list.getAdapter();
		int index = 0;
		
		while(index < thresholds.size())
		{
			DailyThreshold threshold = db.getDayThreshold(thresholds.get(index).getDay());
			threshold.setAmount(adapter.getView(index, null, null).getAmount());		
			db.saveDailyThreshold(threshold);
			
			index++;
		}
		
		//activity.setMonthlyThreshold(monthlyLimit);
		shouldRefreshThresholdList = true;
		Log.i("DailyThresholdDialog", "daily threshold saved...");
		Toast.makeText(_context, "Daily threshold saved !!!", 3).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("DailyThresholdDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		db = TelecomCost.getDatabaseManager();
		setContentView(R.layout.day_range_threshold_dialog);

		setTitle("Daily Threshold");

threshold_list = (ListView) findViewById(R.id.listViewThreshold);
		
		btnClose = (Button) findViewById(R.id.btnClose);
		btnSave = (Button) findViewById(R.id.btnSave);

		btnClose
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View arg0) {
						closeDialog();
					}
				});

		btnSave.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				try {
					saveDailyThreshold();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Log.i("DailyThresholdDialog", "Dialog ohne bundle created....");
	}

	protected void closeDialog() {
		if (shouldRefreshThresholdList) {
			((DailyThresholdListActivity) _context).bindData();
		}
		
		hide();
	}

	@Override
	public void show() {
		Log.i("DailyThresholdDialog", "showing the dialog...");
		super.show();
		Log.i("DailyThresholdDialog", "dialog on screen...");
		bindDailyTreshold();
	}

	@Override
	protected void onStart() {
		Log.i("DailyThresholdDialog", "Starting the dialog...");
		super.onStart();
		Log.i("DailyThresholdDialog", "dialog started...");
	}

	private void bindDailyTreshold() {
		Log.i("DailyThresholdDialog", "binding DailyThresholdDialog...");

		DailyThresholdListActivity activity = (DailyThresholdListActivity) _context;
		MonthlyThreshold currentMonth = activity.getMonth();
		WeeklyThreshold currentWeek = activity.getWeek();
		
		if (currentMonth != null) {
			Log.i("DailyThresholdListActivity",
					"binding DailyThreshold...we get the current weekly threshold from the monthly threshold");
			currentWeek = currentWeek == null ? currentMonth.getWeeklyThresholds().get(0) : currentWeek;
			
			ArrayList<DailyThreshold> thresholds = currentWeek.getDailyThresholds();

			threshold_list.setAdapter(new DailyThresholdEditAdapter(
					_context, R.layout.threshold_edit_view,
					thresholds));
		} else {
			Log.i("DailyThresholdListActivity",
					"cannot bind the daily threshold...set the month and week first");
		}
	}

	@Override
	protected void onStop() {
		Log.i("DailyThresholdDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("DailyThresholdDialog", "dialog stopped...");
	}
}
